package com.roc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static String getLastMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");

		// 获取前月的第一天
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.MONTH, -1);
		String lostMonth = format.format(cal_1.getTime());
		return lostMonth;
	}
	public static String getFirstDayOfLastMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

		// 获取前月的第一天
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.MONTH, -1);
		cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = format.format(cal_1.getTime());
		return firstDay;
	}

	public static String getLastDayOfLastMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}

	public static String getFirstDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// 获取当前月第一天：

		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = format.format(c.getTime());
		return first;
	}

	public static String getLastDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

		// 获取当前月最后一天
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH,
				ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = format.format(ca.getTime());
		return last;
	}
	
	public static String initStartDate(Date date){
		SimpleDateFormat format = new SimpleDateFormat(Constance.DATA_FORMAT_PATTERN_01);
		String startDate = format.format(date);
		
		return startDate;
	}
	public static String initEndDate(){
		SimpleDateFormat format = new SimpleDateFormat(Constance.DATA_FORMAT_PATTERN_01);
		
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.YEAR, ca.get(Calendar.YEAR)+1);
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		String endDate=format.format(ca.getTime());
		return endDate;
	}
	
	/**
	 * 字符日期转DATE
	 * @param dateStr
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date string2Date(String dateStr,String format) throws ParseException{
		
		String formatStr = dateStr;
		if(dateStr==null || "".equals(dateStr)){
			return null;
		}
		if(dateStr.length()>18){//专门处理TIMESTAMP类型
			formatStr = dateStr.substring(0, 19);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(formatStr);
		return date;
	}
	
	
	//处理TIMESTAMP类型
	public static String formatTime(String dateStr){
		
		String formatStr = dateStr;
		if(dateStr==null || "".equals(dateStr)){
			return "";
		}
		if(dateStr.length()>18){//专门处理TIMESTAMP类型
			formatStr = dateStr.substring(0, 19);
		}
		
		return formatStr;
		
	}
	
	/**
	 * 获取前后N天的日期 
	 * @param date
	 * @param count  昨天-1,明天1
	 * @param format
	 * @return
	 */
	public static String getDate(Date date,int count,String format){
		  Calendar  cal  =  Calendar.getInstance();
		  cal.setTime(date);
		  cal.add(Calendar.DATE,count);
		  String yesterday = new SimpleDateFormat(format).format(cal.getTime());
		  return yesterday;
	}
	
	/**
	 * 获取后一年
	 * */
	public static String getYear(Date date,int count,String format){
		  Calendar  cal  =  Calendar.getInstance();
		  cal.add(Calendar.YEAR,count);
		  String yesterday = new SimpleDateFormat(format).format(cal.getTime());
		  return yesterday;
	}


    /**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(String date1 , String date2) throws ParseException    
    {    

	    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");  
	    Date d1=sdf.parse(date1);  
	    Date d2=sdf.parse(date2);  
	 //   return daysBetween(d1,d2);
    	/*
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  */
        Calendar cal = Calendar.getInstance();    
        cal.setTime(d1);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(d2);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }   
	public static void main(String[] args) throws ParseException {
		
		/*String s = getDate(new Date(),0,"yyyy-MM-dd");
		System.out.println(s);
		
		
		List<String> t=new ArrayList<String>();
		t.add("S01");
		t.add("S02");
		t.add("S03");
		System.out.println(Constance.list2SqlStr(t));
		System.out.println("getDate:"+getDate(new Date(),-1,"yyyyMMdd"));*/
//		System.out.println(getYear(new Date(), +1, "yyyy-MM-dd HH:mm:ss"));
	        System.out.println("getDate:"+getDate(string2Date("2016-12-30", "yyyy-MM-dd"),+1,"yyyy-MM-dd"));
	        System.out.println("getDate:"+getDate(new Date(),+1,"yyyy-MM-dd"));
	}
	
	/**
	 * 时间格式转化为指定的字符串格式 
	 * @param aDate
	 * @param format
	 * @return
	 */
    public static final String Date2String(Date aDate,String format) {  
        SimpleDateFormat df = null;  
        String returnValue = "";  
        if (aDate != null) {  
            df = new SimpleDateFormat(format);  
            returnValue = df.format(aDate);  
        }  
        return (returnValue);  
    }  
}
